package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("RulecolumnController")
public class RulecolumnController extends CmiEmaController {

	@RequestMapping("/listRulecolumns")
	public ModelAndView listRulecolumns(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = rulecolumnPaginationService.updatePagination(request, pagination);
		Set<Rulecolumn> rulecolumns = rulecolumnPaginationService.getPaginationRulecolumns(pagination);

		mav.addObject("rulecolumns", rulecolumns);
		mav.addObject("entityName", "rulecolumn");
		mav.addObject("pagination", pagination);
		mav.setViewName("rulecolumn/listRulecolumns.jsp");
		return mav;
	}

	public String listRulecolumns() {
		return "redirect:/listRulecolumns";
	}

	@RequestMapping("/viewRulecolumn")
	public ModelAndView viewRulecolumn(@RequestParam Integer rulecolumnId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("rulecolumn", rulecolumnDAO.findRulecolumnByPrimaryKey(rulecolumnId));
		mav.setViewName("rulecolumn/viewRulecolumn.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteRulecolumns")
	public ModelAndView confirmDeleteRulecolumns(
			@RequestParam(value = "deleteRulecolumnsIds", required = true) Set<Integer> deleteRulecolumnsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Rulecolumn> deleteRulecolumns = rulecolumnDAO.findRulecolumnsByIds(deleteRulecolumnsIds);
		Set<Rulecolumn> referencedRulecolumns = rulecolumnService.loadReferenceRulecolumns(deleteRulecolumns);
		boolean hasReferenced;
		if (referencedRulecolumns.size() == 0) {
			mav.addObject("deleteEntitys", deleteRulecolumns);
			hasReferenced = false;
		} else {
			deleteRulecolumns.removeAll(referencedRulecolumns);
			mav.addObject("referencedEntitys", referencedRulecolumns);
			mav.addObject("deleteEntitys", deleteRulecolumns);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Rulecolumn");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listRulecolumns");
		return mav;
	}

	@RequestMapping("/deleteRulecolumn")
	public String deleteRulecolumn(@RequestParam Integer rulecolumnId) {
		Rulecolumn rulecolumn = rulecolumnDAO.findRulecolumnByPrimaryKey(rulecolumnId);
		rulecolumnService.deleteRulecolumn(rulecolumn);
		return "forward:/listRulecolumns";
	}

	@RequestMapping("/confirmDeleteRulecolumn")
	public ModelAndView confirmDeleteRulecolumn(@RequestParam Integer rulecolumnId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("rulecolumn", rulecolumnDAO.findRulecolumnByPrimaryKey(rulecolumnId));

		mav.setViewName("rulecolumn/deleteRulecolumn.jsp");

		return mav;
	}

	@RequestMapping("/deleteRulecolumns")
	public String deleteRulecolumns(
			@RequestParam(value = "deleteRulecolumnsId", required = true) Set<Integer> deleteRulecolumnsId) {
		rulecolumnService.deleteRulecolumns(deleteRulecolumnsId);
		return "forward:/listRulecolumns";
	}

	@RequestMapping("/editRulecolumn")
	public ModelAndView editRulecolumn(@RequestParam Integer rulecolumnId) {

		ModelAndView mav = new ModelAndView();

		Rulecolumn rulecolumn = rulecolumnDAO.findRulecolumnByPrimaryKey(rulecolumnId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("rulecolumn", rulecolumn);

		// 把所有的引用rulecolumn对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(rulecolumnService.loadRulecolumnObjectReferencedSet(rulecolumn));

		// 将编辑页面rulecolumn添加到mav中
		mav.setViewName("rulecolumn/editRulecolumn.jsp");
		return mav;
	}

	@RequestMapping("/newRulecolumn")
	public ModelAndView newRulecolumn() {
		ModelAndView mav = new ModelAndView();
		Rulecolumn rulecolumn = new Rulecolumn();

		mav.addObject("rulecolumn", rulecolumn);

		// 把所有的引用rulecolumn对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(rulecolumnService.loadRulecolumnObjectReferencedSet(rulecolumn));

		mav.addObject("newFlag", true);
		mav.setViewName("rulecolumn/editRulecolumn.jsp");

		return mav;
	}

	@RequestMapping("/saveRulecolumn")
	public ModelAndView saveRulecolumn(@ModelAttribute Rulecolumn rulecolumn) {

		ModelAndView mav = new ModelAndView();

		Rulefield rulefieldTemp = rulecolumn.getRulefield();
		Rulefield rulefield = rulefieldTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(rulefieldDAO.findRulefieldById(rulefieldTemp.getId()));

		Rulecolumn existingRulecolumn = CmiHibernateProxyUtil.deproxy(rulecolumnDAO.findRulecolumnByPhysicalPrimaryKey(
				rulefield.getId(), rulecolumn.getExcelcolumn(), rulecolumn.getPartion()));

		rulecolumn.setRulefield(rulefield);
		// 新建记录
		if (rulecolumn.getId() == null) {
			if (rulecolumn.equals(existingRulecolumn)) {

				mav.addAllObjects(rulecolumnService.loadRulecolumnClassReferencedSet(rulecolumn));
				// mav = editContinue(mav, rulecolumn);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("rulecolumn/editRulecolumn.jsp");
			} else {
				rulecolumnService.saveRulecolumn(rulecolumn);
				mav.setViewName("forward:/listRulecolumns");
			}
		}
		// 编辑记录
		else {
			Rulecolumn originRulecolumn = CmiHibernateProxyUtil
					.deproxy(rulecolumnDAO.findRulecolumnByPrimaryKey(rulecolumn.getId()));
			if (!rulecolumn.equals(originRulecolumn) && rulecolumn.equals(existingRulecolumn)) {
				mav.addAllObjects(rulecolumnService.loadRulecolumnClassReferencedSet(rulecolumn));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("rulecolumn/editRulecolumn.jsp");
			} else {
				rulecolumnService.saveRulecolumn(rulecolumn);
				mav.setViewName("forward:/listRulecolumns");
			}
		}
		return mav;
	}

	@RequestMapping("/rulecolumnController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
